﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Utils
{
    public class DataFilter
    {
        int Size = 0;
        double[] DataArray;
        int index = 0;
        public DataFilter(int size)
        {
            Size = size;
            DataArray = new double[Size];
        }

        public void Insert(double data)
        {
            DataArray[index] = data;
            index = (index + 1) % Size;
        }

        public double Average()
        {
            return DataArray.Average();
        }
    }

    public class DataQueue
    {
        public int Size { get; private set; } = 5;

        /// <summary>
        /// 集合的数据
        /// </summary>
        public Queue<double> DataArray { get; } 


        public DataQueue(int size=5)
        {
            Size = size;
            DataArray = new Queue<double>(Size);
        }

        public void Enqueue(double data)
        {
            if(DataArray.Count>= Size)
            {
                DataArray.Dequeue();
                Enqueue(data);
                return;
            }
            DataArray.Enqueue(data);
        }

        /// <summary>
        /// 平均数
        /// </summary>
        /// <returns></returns>
        public double Average()
        {
            if(DataArray.Count>= 0)
            {
                return 0;
            }
            return DataArray.Average();
        }
    }
}
